$(function(){
	var empMsgList;
	empMsgList=$("#empMsgList").datagrid({
		toolbar:"#toolbar",
		fit : true,
		fitColumns : true,
  		striped:true,
  		singleSelect:'true',
		pagination:'true',
		idField : 'emp_id',
		rownumbers : true,
		columns : [ [
		{field:'checkbox',title:'check',width:10, checkbox:true },
		{field:'emp_id', title:'emp_id', hidden:true },
		{field:'real_name', title:'姓名', width:40 },
		{field:'user_name', title:'用户名', width:40 },
		{field:'abbr_name', title:'机构', width:40 },
		{field:'dept_name', title:'部门', width:40 },
		{field:'join_time', title:'加入公司时间', width:40 },	
		{field:'emp_id', title:'emp_id', hidden:true },
			]]
		
	});
	$("#organ_id").combobox( { 
			url : '/admin/getAllOrgan',
			width:fixWidth(0.12),
			panelHeight:'auto',
			valueField : 'organId',
			textField : 'abbrName',
			 onSelect: function(obj){ 
			$('#organ_id').val(obj.organId);
			$("#childOrgan").combobox( {
				url : '/admin/getAllOrganByOrganId?organId='+obj.organId+'&classId=3'
			});
			if(obj.organId!=""||obj.organId!=null){
				$("#Fdept").combobox( {
					url : '/admin/getDeptByOrganId?organId='+obj.organId
				});
				
			}
			
        			}
				});
	
	
	
	$("#childOrgan").combobox( { 
			width:fixWidth(0.12),
			panelHeight:'auto',
			valueField : 'organId',
			textField : 'abbrName',
			 onSelect: function(obj){ 
			$('#childOrgan').val(obj.organId);
			$("#subChildOrgan").combobox( {
				url : '/admin/getAllOrganByOrganId?organId='+obj.organId+'&classId=4'
			});
        	}
		});
	
	$("#subChildOrgan").combobox( { 
		valueField : 'organId',
		width:fixWidth(0.12),
		panelHeight:'auto',
		textField : 'abbrName',
		 onSelect: function(obj){ 
		$('#subChildOrgan').val(obj.organId);
		
    	}
	});
	
var dept;
dept=$("#Fdept").combobox( {
	width:fixWidth(0.12),
	panelHeight:'auto',
	valueField : 'dept_id',
	textField : 'dept_name',
	onSelect: function(obj){ 
			$('#Fdept').val(obj.dept_id);	
			
        		}
			});


	
//搜索框函数
$("#btnSearch").bind('click',function(){
	$("#empMsgList").datagrid({
		url:'/admin/getUserByParams',
		queryParams: {
		'organId':$("#organ_id").val(),
		'childOrgan':$("#childOrgan").val(),
		'subChildOrgan':$("#subChildOrgan").val(),
		'dept':$("#Fdept").val(),
		'userName':$("#user_name").val(),
		'realName':$("#real_name").val()
		
		}
		});
});

var addEmpDialog;
addEmpDialog=$("#addEmpDialog").dialog({
	title : '新增用户',
	width : fixWidth(0.7),
	height : fixHeight(0.8),
	closed : true,											
	cache : false,
	iconCls : 'icon-add',									
	modal : true,
	resizable:true,
	onClose:function(){						
		$("#addEmpForm").form("clear");
		
	},		
	buttons : [ {							
		iconCls : 'icon-ok',
		text : '提交',
		handler : function() {				
				$("#addEmpForm").form('submit', {	
					url : '/admin/addOrUpdateUser',
					onSubmit : function() {
						//$.parser.parse(paraValueList);
						return $(this).form('validate');
					},
				success : function(data) {
					if (data == 'ok') {
						$('#addEmpDialog').dialog("close");
						messageSuccess(data);
					} else {
						messageFail(data);
							}
						}
					});
				}
			}]		
	});

$("#btnAddEmp").bind('click',function(){
	addEmpDialog.dialog('open');
	
});

$("#organId").combobox({
	url:"/admin/getAllOrgan",
	width:fixWidth(0.12),
	panelHeight:'auto',
	valueField : 'organId',
	textField : 'abbrName',
	onSelect: function(obj){ 
			$('#organId').val(obj.organId);	
			if(obj.organId!=""||obj.organId!=null){
				$("#dept").combobox( {
					url : '/admin/getDeptByOrganId?organId='+obj.organId
				});
				
			}
        		}
			});
$("#dept").combobox( {
	width:fixWidth(0.12),
	panelHeight:'auto',
	valueField : 'dept_id',
	textField : 'dept_name',
	onSelect: function(obj){ 
			$('#dept').val(obj.organId);	
			
        		}
			});
	
	$("#btnUpdate").bind('click',function(){
		var row=empMsgList.datagrid('getSelected');
		if(row){
			
			$.ajax({
				url:"/admin/getSelectedUSerMassage",
				type:"get",
				data:{empId:row.emp_id}, 	
				dataType:"json",
				success:function(data){
					if(data!=null)
					{
						$("#userName").val(data.USER_NAME);
						$("#realName").val(data.REAL_NAME);
						$("#abbr_name").val(data.ABBR_NAME);
						$("#email").val(data.EMAIL);
						$("#gender").val(data.GENDER);
						$("#birthday").val(new Date(data.BIRTHDAY).Format("yyyy-MM-dd"));
						$("#certi_type").combobox("setValue",data.CERTI_TYPE);
						$("#certi_code").val(data.CERTI_CODE);
						$("#join_time").val(new Date(data.JOIN_TIME).Format("yyyy-MM-dd"));
						$("#emp_code").val(data.EMP_CODE);
						$("#talk_back").val(data.TALK_BACK);
						$("#celler").val(data.CELLER);
						$("#userDisable").val(data.USER_DISABLE);
						$("#user_status").val(data.USER_STATUS);
						$("#passwordChange").val(new Date(data.PASSWORD_CHANGE).Format("yyyy-MM-dd"));
						$("#defaultLang").val(data.DEFAULT_LANG);
						$("#is_admin").val(data.IS_ADMIN);
						$("#special_id").combobox("setValue",data.SPECIAL_ID);
						$("#user_name").val(data.USER_NAME);
						$("#user_name").val(data.USER_NAME);
						
					}else{
						$.messager.show({
						title:'提示',
						msg:'获取数据失败！'+data,
						timeout:3000,
						showType:'slide'
							});
						}
					}
				});
			
			
			addEmpDialog.dialog('open');
		}else{
			alert("亲，请先选择一行！");
		}
		
		
	});

$("#certi_type").combobox({
	url:'/admin/getCertiType',    
    valueField:'TYPE_ID',    
    textField:'TYPE' , 
	width:100,
	panelHeight:'auto'
});

$("#special_id").combobox({
	url:'/admin/getSpecialId',    
    valueField:'SPECIAL_ID',    
    textField:'SPECIAL_DESC' , 
	width:100,
	panelHeight:'auto'
});

	
});


//网页可见区域宽百分比转换
function fixWidth(percent)  
{  
    return document.body.clientWidth * percent ; //这里你可以自己做调整  
}  
//网页可见区域高百分比转换
function fixHeight(percent)  
{  
    return document.body.clientHeight * percent ; //这里你可以自己做调整  
}  



//操作成功滑框
function  messageSuccess(data) {
		$.messager.show( {		
		title : '我的消息',
		msg : '操作成功！',
		timeout : 3000,		
		showType : 'slide'	
		});
	} 
//操作失败滑框
function messageFail(data){
		$.messager.show( {		
		title : '消息提示',
		msg : data,
		timeout : 5000,		
		showType : 'slide'	
		});
	}

//例子： javascript Date format(js日期格式化)
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}